#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
plots Binder cumulant
'''

from __future__ import division

import matplotlib.pyplot
import sys

import common
import os
import math

path = os.path.join('..', '..', 'results', 'triang', 'J_-1.0' )

#tList = [3, 6 ,9, 24, 30]

cumulant3 = []

cumulant6 = []

cumulant9 = []

cumulant24 = []

cumulant30 = []


for tB in os.listdir(path):  
        
    B = float(tB.replace('B_',  ''))
    
    try:
        OP2_24 = float(open(os.path.join(path, tB, 'L_24', 'OPT2' ,'T0.05')).read())
        OP4_24 = float(open(os.path.join(path, tB, 'L_24', 'OPT4' ,'T0.05')).read())
    except:
        pass
    else:
        cumulant24 += [ ( B, 1 - OP4_24 / (3 * OP2_24**2 ) ) ]
    
    try:
        OP2_30 = float(open(os.path.join(path, tB, 'L_30', 'OPT2' ,'T0.05')).read())
        OP4_30 = float(open(os.path.join(path, tB, 'L_30', 'OPT4' ,'T0.05')).read())
    except:
        pass
    else:
        cumulant30 += [ (B, 1 - OP4_30 / (3 * OP2_30**2 ) )]
        
    try:
        OP2_3 = float(open(os.path.join(path, tB, 'L_3', 'OPT2' ,'T0.05')).read())
        OP4_3 = float(open(os.path.join(path, tB, 'L_3', 'OPT4' ,'T0.05')).read())
    except:
        pass
    else:
        cumulant3 += [ (B, 1 - OP4_3 / (3 * OP2_3**2 ) )]
    
    try:
        OP2_6 = float(open(os.path.join(path, tB, 'L_6', 'OPT2' ,'T0.05')).read())
        OP4_6 = float(open(os.path.join(path, tB, 'L_6', 'OPT4' ,'T0.05')).read())
    except:
        pass
    else:
        cumulant6 += [ (B, 1 - OP4_6 / (3 * OP2_6**2 ) )]
    
    try:
        OP2_9 = float(open(os.path.join(path, tB, 'L_9', 'OPT2' ,'T0.05')).read())
        OP4_9 = float(open(os.path.join(path, tB, 'L_9', 'OPT4' ,'T0.05')).read())
    except:
        pass
    else:
        cumulant3 += [ (B, 1 - OP4_9 / (3 * OP2_9**2 ) )]
    
    
    cumulant3.sort()
    cumulant6.sort()
    cumulant9.sort()
    cumulant24.sort()
    cumulant30.sort()
    
    
    x3 = [tx[0] for tx in cumulant3]
    y3 = [tx[1] for tx in cumulant3]
    
    x6 = [tx[0] for tx in cumulant6]
    y6 = [tx[1] for tx in cumulant6]
    
    x9 = [tx[0] for tx in cumulant9]
    y9 = [tx[1] for tx in cumulant9]
    
    x24 = [tx[0] for tx in cumulant24]
    y24 = [tx[1] for tx in cumulant24]
    
    
    x30 = [tx[0] for tx in cumulant30]
    y30 = [tx[1] for tx in cumulant30]
    

matplotlib.pyplot.plot(x3, y3, label = '$L = 3$')
matplotlib.pyplot.plot(x6, y6, label = '$L = 6$')
matplotlib.pyplot.plot(x9, y9, label = '$L = 9$')    
matplotlib.pyplot.plot(x24, y24, label = '$L = 24$')
matplotlib.pyplot.plot(x30, y30, label = '$L = 30$')
    
matplotlib.pyplot.legend()
matplotlib.pyplot.show()